package com.llianejapan.RomaTo;

import java.net.URLEncoder;

import com.llianejapan.KanjiLists.*;
import com.llianejapan.LlianeGlobal;
import com.llianejapan.R;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;

public class RomaToKanji extends ListActivity{
	public ArrayAdapter<String> mStrings;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		setTheme(android.R.style.Theme_Black_NoTitleBar);
		foundKanji = 1;
		super.onCreate(savedInstanceState);
		mStrings = new ArrayAdapter<String>(this, R.layout.romatokanji);
		for (int kln = 0; kln < KANJIML.kmlr.length; kln++)
		{
			if (KANJIML.kmlr[kln][KANJIML.kmlr[kln].length - 1][0].compareTo(LlianeGlobal.stringBuffer.toString()) <= 0)
				continue;
			for (int i = 0; i < KANJIML.kmlr[kln].length ; i++)
			{
				if (LlianeGlobal.stringBuffer.toString().equals(KANJIML.kmlr[kln][i][0]))
					mStrings.add(KANJIML.kmlr[kln][i][1]);
			}
		}
		if (mStrings.getCount() == 0)
		{
			mStrings.add("Kanji not found");
			foundKanji = 0;
		}
		setListAdapter(mStrings);
	}
	@Override
	protected void onListItemClick(android.widget.ListView l, View v, int position, long id)
	{	
		if (foundKanji != 0)
			LlianeGlobal.stringBuffer = mStrings.getItem((int) id );
		else
			LlianeGlobal.stringBuffer = "";
		if (forTranslate == 1)
		{
			LlianeGlobal.ime_2.append(LlianeGlobal.stringBuffer);
			if (foundKanji != 0)
				LlianeGlobal.ime_1.setText("");
		}
		if (forTranslate == 2)
		{
			LlianeGlobal.wv2.append(LlianeGlobal.stringBuffer);
			if (foundKanji != 0)
				LlianeGlobal.wv1.setText("");
		}
		else
		{
			LlianeGlobal.roma2kanji_showKanji.setText(LlianeGlobal.stringBuffer);
			LlianeGlobal.wvurl = 
				"http://ks363797.kimsufi.com/anime/k/"+
			URLEncoder.encode(LlianeGlobal.stringBuffer)
			+".htm";
		}
		finish ();
	}
	public static int forTranslate; //1 = yes, 0 = forKanji
	public static int foundKanji;
}